/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.util;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;

import com.phonebooksharing.dto.LocationItem;

public class LocationUtil {

    public static Location getCurrentLocation(Context cx) {
        final LocationManager lm = (LocationManager)cx.getSystemService(Context.LOCATION_SERVICE); 
        List<String> lp = lm.getProviders(true);
        if(lp == null || lp.size() <= 0) {
            //TODO: throw a throable exception
            return null;
        }
//        Location lc = lm.getLastKnownLocation(lp.get(0));
//        if(lc != null) return lc;
//        return lc;
        
        Location loc = new Location(lp.get(0));
        loc.setLatitude(48.15892);
        loc.setLongitude(12.18016);
        return loc;
    }
    
    public static String getWhereAt(Context cx) throws IOException {
        Address a = getCurrentAddress(cx);
        final String city = a.getPostalCode() + " " + a.getLocality() + " " + a.getCountryName();
//        final String city = "some place";
        return city;
    }
    
    public static LocationItem getCurrentLocationItem(Context cx) throws IOException {
        LocationItem li = new LocationItem(Global.authItem);
        Location l = getCurrentLocation(cx);
        li.latitute = l.getLatitude();
        li.longitude = l.getLongitude();
        li.location = getAddress(cx, l);
        
        return li;
    }
    
    public static LocationItem getManualLocationItem(Context cx, String address) throws IOException {
        LocationItem li = new LocationItem(Global.authItem);
        Address a = getAddress(cx, address);
        
        li.latitute  = a.getLatitude();
        li.longitude = a.getLongitude();
        li.location  = a.getPostalCode() + " " + a.getLocality() + " " + a.getCountryName();
        
        return li;
    }
    
    public static String getAddress(Context cx, Location l) throws IOException {
//        Geocoder g = new Geocoder(cx);
//        Address a = g.getFromLocation(l.getLatitude(), l.getLongitude(), 1).get(0);
//        return a.getPostalCode() + " " + a.getLocality() + " " + a.getCountryName();
        return "Fake address";
    }
    
    public static Address getCurrentAddress(Context cx) throws IOException {
        Location l = getCurrentLocation(cx);
        Geocoder g = new Geocoder(cx);
        return g.getFromLocation(l.getLatitude(), l.getLongitude(), 1).get(0);
    }
    
    public static Address getAddress(Context cx, double latitude, double longitude) throws IOException {
        Geocoder g = new Geocoder(cx);
        return g.getFromLocation(latitude, longitude, 1).get(0);
    }
    
    public static Address getAddress(Context cx, String address) throws IOException {
        Geocoder g = new Geocoder(cx);
        return g.getFromLocationName(address, 1).get(0);
    }
}
